<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>【项目】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目" prop="projectid" :required="true" :rules="rules.validateRequire" :disabled="disableEditSubmit || disableSave">
            <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目编号">
            <el-input v-model="model.projectcode" :disabled="true"> </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同金额">
            <el-input-number v-model="model.projectcontractmoney" controls-position="right" :step="1" :precision="2" style="width:95%" :disabled="true"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目金额">
            <el-input-number v-model="model.projectmoney" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%" :disabled="true"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="已开票金额">
            <el-input-number v-model="model.projectbillmoney" controls-position="right" :step="1" :precision="2" style="width:95%" :disabled="true"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="已收款金额">
            <el-input-number v-model="model.projectrecbillmoney" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%" :disabled="true"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>【业务类型】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="业务类型" prop="category" :rules="rules.validateRequire">
            <el-select v-model="model.category" filterable clearable @change="currentDatas" :disabled="disableEditSubmit || disableSave">
              <el-option v-for="item in codeModel.categoryList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" :disabled="disableEditSubmit || disableSave">
          <el-form-item label="原发票号" prop="oldinvoicecode" v-show="isCancelAndReadd" :rules="isCancelAndReadd ? rules.validateRequire : { required: false }">
            <el-input v-model="model.oldinvoicecode" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmInvoiceRecordRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="原发票金额" prop="oldinvoicebillmoney" v-show="isCancelAndReadd" :disabled="disableEditSubmit || disableSave">
            <el-row>
              <el-col :span="23">
                <el-input v-model="model.oldinvoicebillmoney" class="money-input" :disabled="true"></el-input>
              </el-col>
              <el-col :span="1">
                <span>元</span>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发票是否认证" prop="invoicecheckflag" v-show="isCancelAndReadd" :rules="isCancelAndReadd ? rules.validateRequire : { required: false }">
            <el-select v-model="model.invoicecheckflag" filterable clearable :disabled="disableEditSubmit || disableSave">
              <el-option v-for="item in codeModel.invoicecheckflagList" :key="item.code" :label="item.name" :value="parseInt(item.code)" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="isCancelAndReadd">
        <el-col :span="6"> <span style="color:red" v-show="isCancelAndReadd && this.model.invoicecheckflag === 1">*</span>《红字增值税专用发票信息表》编号 </el-col>
        <el-col :span="18">
          <el-form-item prop="invoicenoofredtax" label-width="0" v-show="isCancelAndReadd" :rules="isCancelAndReadd && this.model.invoicecheckflag === 1 ? rules.validateRequire : { required: false }">
            <el-input v-model="model.invoicenoofredtax" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-form class="tl-form" ref="categoryform" :model="model" :rules="rules" label-width="150px" v-show="model.category == '2' || model.category == '3'" :disabled="disableEditSubmit || disableSave"> -->
      <!-- <el-row>
        <el-col :span="12">
          <el-form-item label="原发票邮寄单号" prop="oldnumber" v-show="isCancelAndReadd">
            <el-input v-model="model.oldnumber" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原客户名称" prop="oldcustomername" v-show="isCancelAndReadd">
            <el-input v-model="model.oldcustomername" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row> -->
      <!-- <el-row>
        <el-col :span="12">
          <el-form-item label="原开票日期" prop="oldinvoicedate" v-show="isCancelAndReadd">
            <el-date-picker v-model="model.oldinvoicedate" type="date" placeholder="原开票日期" value-format="yyyy-MM-dd" disabled></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="原开票金额" prop="oldinvoicemoney" v-show="isCancelAndReadd">
            <el-input-number v-model="model.oldinvoicemoney" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%" :disabled="true"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row> -->
      <!-- </el-form> -->
      <el-row>
        <el-col :span="24">
          <el-form-item label="重开原因" prop="reinvoicereason" v-show="model.category == '3'" :rules="model.category == '3' ? rules.validateRequire : { required: false }">
            <el-input v-model="model.reinvoicereason" type="textarea" :rows="2" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="作废原因" prop="cancelreason" v-show="model.category == '2'" :rules="model.category == '2' ? rules.validateRequire : { required: false }">
            <el-input v-model="model.cancelreason" type="textarea" :rows="2" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="作废发票回寄信息" prop="cancelbilllogisticsinfo" v-show="isCancelAndReadd" :rules="isCancelAndReadd && this.model.invoicecheckflag === 0 ? rules.validateRequire : { required: false }">
            <el-input v-model="model.cancelbilllogisticsinfo" type="textarea" :rows="2" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-form class="tl-form" ref="invoiceform" :model="model" :rules="rules" label-width="150px" v-show="model.category == '1' || model.category == '3'" :disabled="disableEditSubmit || disableSave"> -->
      <el-row>
        <el-col :span="24" v-show="isAddAndReadd">
          <el-divider content-position="left"><span>【发票】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="发票开具单位" prop="invoiceorgname" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <tl-ref-org v-model="model.invoiceorgname" :initDisplayData.sync="model.invoiceorgname" dataType="code" :singleSelect="true" @confirm="onSelectOrg" :disabled="disableEditSubmit || disableSave" :externalQueryParam="{ qry_enabled: 1 }"></tl-ref-org>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发票所属部门" prop="invoicedeptname" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }" :disabled="disableEditSubmit || disableSave">
            <tl-ref-dept v-model="model.invoicedeptname" :initDisplayData.sync="model.invoicedeptname" dataType="code" nameType="fullname" :singleSelect="true" @confirm="onSelectDept" :disabled="disableEditSubmit || disableSave" :externalQueryParam="{ qry_enabled: 1 }"></tl-ref-dept>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20" :offset="3" v-show="isAddAndReadd">
          <el-alert title="发票类型为税率是3%或者9%的发票时，必须在发票备注里按照格式写明项目名称和项目地点" type="warning" :closable="false" show-icon>
          </el-alert>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="发票类型" prop="invoicetype" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-select v-model="model.invoicetype" filterable clearable :disabled="disableEditSubmit || disableSave">
              <el-option v-for="item in codeModel.invoicetypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开票金额" prop="invoicemoney" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input-number v-model="model.invoicemoney" controls-position="right" :step="1" :min="0" :precision="2" style="width:95%" @change="changeInvoicemoney" :disabled="disableEditSubmit || disableSave"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="isAddAndReadd">
        <el-col :span="12">
          <el-form-item label="单价" prop="invoiceamount" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input-number v-model="model.invoiceamount" controls-position="right" :step="1" :min="0" :precision="6" style="width:95%" @change="changeInvoiceamount" :disabled="disableEditSubmit || disableSave"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="数量" prop="invoicecount" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input-number v-model="model.invoicecount" controls-position="right" :step="1" :min="0" :precision="2" @change="changeInvoicecount" style="width:100%" :disabled="disableEditSubmit || disableSave"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="单位" prop="invoiceunit" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input v-model="model.invoiceunit" :disabled="disableEditSubmit || disableSave" placeholder="台、项等"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="isAddAndReadd">
        <el-col :span="12">
          <el-form-item label="开票名称" prop="invoicename" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input v-model="model.invoicename" :maxlength="50" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="税收类别" prop="explain" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-select v-model="model.explain" filterable clearable :disabled="disableEditSubmit || disableSave">
              <el-option v-for="item in codeModel.invgoodscodeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="isAddAndReadd">
        <el-col :span="24">
          <el-form-item label="发票备注（内容体现在发票上）" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="2" :placeholder="placeholder" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <!-- <el-form-item label="开票要求" prop="demand" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }"> -->
          <el-form-item label="开票要求" prop="demand" v-show="isAddAndReadd">
            <el-input v-model="model.demand" type="textarea" :rows="2" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="24">
          <el-form-item label="开票说明" prop="explain" v-show="isAddAndReadd">
            <el-input v-model="model.explain" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="customername" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input v-model="model.customername" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmCustomerRefDialog" v-if="!disableEditSubmit && !disableSave" :disabled="disableEditSubmit || disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
                        <el-form-item label="开票标识" prop="invoiceflag" :required="true" :rules="rules.validateRequire">
                            <el-input v-model="model.invoiceflag" :disabled="true" class="tl-ref-selector">
                                <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmCustomerBillRefDialog" v-if="!disableEditSubmit"></el-button>
                            </el-input>
                        </el-form-item>
                    </el-col> -->
        <el-col :span="12">
          <el-form-item label="客户ERP编号" prop="erpcode" v-show="isAddAndReadd">
            <el-input v-model="model.erpcode" :readonly="true" class="is-disabled" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="企业名称" prop="companyname" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }">
            <el-input v-model="model.companyname" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmCustomerBillRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="ERP销售单号" prop="erpdocno" v-show="isAddAndReadd" :disabled="disableEditSubmit || disableSave">
            <el-input v-model="model.erpdocno" :readonly="true" class="is-disabled" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="企业地址" prop="companyaddress" v-show="isAddAndReadd">
            <el-input v-model="model.companyaddress" :readonly="true" class="is-disabled" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="税号" prop="taxno" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }" :disabled="disableEditSubmit || disableSave">
            <el-input v-model="model.taxno" :readonly="true" class="is-disabled" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="companytel" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }" :disabled="disableEditSubmit || disableSave">
            <el-input v-model="model.companytel" :readonly="true" class="is-disabled" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="开户行" prop="bankname" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }" :disabled="disableEditSubmit || disableSave">
            <el-input v-model="model.bankname" :readonly="true" class="is-disabled" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行账户" prop="bankaccount" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }" :disabled="disableEditSubmit || disableSave">
            <el-input v-model="model.bankaccount" :readonly="true" class="is-disabled" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="账期方式" prop="paymethod" :rules="rules.validateRequire" v-if="isAddAndReadd">
            <el-row>
              <el-col :span="22">
                <el-input v-model="model.paymethod" :disabled="true" class="tl-ref-selector">
                  <el-button slot="append" type="primary" icon="el-icon-search" @click="openTlErpAccountPeriodDialog"></el-button>
                </el-input>
              </el-col>
              <el-col :span="2">
                <tl-help>
                  如果项目只有一个合同，则将合同中的账期方式、账期名称、ERP来源编码自动带出；
                  如果项目有多个合同，则需要手动选择。
                </tl-help>
              </el-col>
            </el-row>
            <!-- <erp-account-period v-model="model.paymethod" :initDisplayData.sync="model.paymethod" :singleSelect="true"></erp-account-period> -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="账期名称" prop="payname" v-show="isAddAndReadd">
            <el-input v-model="model.payname" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否发送快递" prop="sendflag" v-show="isAddAndReadd" :rules="rules.validateRequire">
            <el-radio-group v-model="model.sendflag" :disabled="disModifyFlag">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="ERP来源编码" prop="erpsourcecode" v-show="isAddAndReadd">
            <el-input v-model="model.erpsourcecode" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收件人" prop="receiver" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }" :disabled="disableEditSubmit || disableSave">
            <el-input v-model="model.receiver" :readonly="false" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收件人电话" prop="receivertel" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }" :disabled="disableEditSubmit || disableSave">
            <el-input v-model="model.receivertel" :readonly="false" :disabled="disableEditSubmit || disableSave"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="邮寄地址" prop="detailmailaddress" v-show="isAddAndReadd" :rules="isAddAndReadd ? rules.validateRequire : { required: false }" :disabled="disableEditSubmit || disableSave">
            <el-cascader size="large" :options="areaData" v-model="selectedAreas" @change="changeArea" style="width:100%" :disabled="disableEditSubmit || disableSave"> </el-cascader>
            <el-input v-model="model.detailmailaddress" placeholder="请输入详细地址" :disabled="disableEditSubmit || disableSave"></el-input>
            <!-- <el-input v-model="model.mailaddress" :readonly="false" class="is-disabled"></el-input> -->
          </el-form-item>
        </el-col>
      </el-row>
      <el-form class="tl-form" ref="dealform" :model="model" :rules="rules" label-width="120px" v-show="model.state == '9' && model.invoiceno != null && isAddAndReadd">
        <el-row>
          <el-col :span="24">
            <el-divider content-position="left"><span>【发票邮寄】相关信息</span></el-divider>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="发票号码" prop="invoiceno" v-show="isAddAndReadd">
              <el-input v-model="model.invoiceno" :disabled="disModifyFlag"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票金额" prop="invoicebillmoney" v-show="isAddAndReadd">
              <el-row>
                <el-col :span="23">
                  <el-input v-model="model.invoicebillmoney" class="money-input" :disabled="disableEditSubmit || disableSave"></el-input>
                </el-col>
                <el-col :span="1">
                  <span>元</span>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
                        <el-form-item label="签收单是否返回" prop="isinvoicereturn">
                            <el-select v-model="model.isinvoicereturn" filterable clearable>
                                <el-option v-for="item in codeModel.isinvoicereturnList" :key="item.code" :label="item.name" :value="item.code" />
                            </el-select>
                        </el-form-item>
                    </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开票日期" prop="invoicedate" :disabled="disableEditSubmit || disableSave">
              <el-date-picker v-model="model.invoicedate" type="date" placeholder="开票日期" value-format="yyyy-MM-dd" :disabled="disableEditSubmit || disableSave"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="寄出日期" prop="maildate" :disabled="disableEditSubmit || disableSave">
              <el-date-picker v-model="model.maildate" type="date" placeholder="寄出日期" value-format="yyyy-MM-dd" :disabled="disableEditSubmit || disableSave"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="快递单号" prop="couriernumber">
              <el-input v-model="model.couriernumber" :disabled="disModifyFlag"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="快递方式" prop="courier">
              <el-select v-model="model.courier" filterable clearable :disabled="disModifyFlag">
                <el-option v-for="item in codeModel.courierList" :key="item.code" :label="item.name" :value="item.code" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-collapse>
        <el-collapse-item title="【单据】相关信息">
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请部门" prop="deptname" :disabled="disableEditSubmit || disableSave">
                <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{ qry_enabled: 1 }"></tl-ref-dept>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请公司" prop="orgname" :disabled="disableEditSubmit || disableSave">
                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请人" prop="username" :disabled="disableEditSubmit || disableSave">
                <tl-ref-user v-model="model.username" :initDisplayData.sync="model.username" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请人工号" prop="usercode" :disabled="disableEditSubmit || disableSave">
                <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单据日期" prop="docdate" :rules="rules.validateRequire" :disabled="disableEditSubmit || disableSave">
                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交时间" prop="submitdate" :disabled="disableEditSubmit || disableSave">
                <el-date-picker v-model="model.submitdate" type="datetime" placeholder="提交时间" disabled></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="审批流程" prop="flowid" :disabled="disableEditSubmit || disableSave">
                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"> </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="state" :disabled="disableEditSubmit || disableSave">
                <el-input v-model="model.dispstate" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <tl-approval-history ref="history" sourceType="PmInvoiceApply" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit" v-if="uiOperateState <= 2">取消</el-button>
      <el-button type="primary" @click="doSubmitEditForModify" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disSaveForModify">
        <svg-icon icon-class="save" />保存
      </el-button>
      <el-button type="primary" :disabled="disableJumpContract" @click="jumpContract(model.projectid)" v-if="!disableEditSubmit">项目合同</el-button>
      <el-button type="primary" @click="doModify" style="padding-left:2px" v-if="model.state===9 && user.code===model.modifyinvoiceusercode">
        <svg-icon icon-class="el-icon-edit" />修改单据信息
      </el-button>
      <tl-attachment sourceType="PmInvoiceApply" :sourceId="model.id" :disabled="disableAttachment" btnTitle="开票文件"></tl-attachment>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
        <svg-icon icon-class="send" />提交审批
      </el-button>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableDelApproval" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button>
    </div>
    <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
    <tlpm-invoice-record-ref ref="tlpmInvoiceRecordRef" @confirm="getpmInvoiceRecordRefData" :isSingleSelect="false" :externalQueryParam="{ qry_projectid: model.projectid, qry_iscancel: '0' }"></tlpm-invoice-record-ref>
    <!-- <tlpm-contract-ref ref="pmContractRef" @confirm="getpmContractRefData" :isSingleSelect="true" :externalQueryParam="{qry_projectid:model.projectid}"></tlpm-contract-ref> -->
    <tlpm-customer-ref ref="pmCustomerRef" @confirm="getpmCustomerRefData" :isSingleSelect="true"></tlpm-customer-ref>
    <tlpm-customer-bill-ref ref="pmCustomerBillRef" @confirm="getpmCustomerBillRefData" :isSingleSelect="true" :externalQueryParam="{ qry_customerid: model.customerid }"></tlpm-customer-bill-ref>
    <tl-erp-account-period ref="tlErpAccountPeriod" :invoiceQuery="1" v-on:erpAccountPeriodDataClear="erpAccountPeriodDataClear" @confirm="getErpAccountPeriodData" :externalQueryParam="{ projectidfrominvoice: model.projectid }"></tl-erp-account-period>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/refList.vue'
// import tlpmContractRef from '@/views/ProjectManage/pmContract/refList.vue'
import tlpmCustomerRef from '@/views/ProjectManage/pmCustomer/refList.vue'
import tlpmInvoiceRecordRef from '@/views/ProjectManage/pmInvoiceRecord/refList.vue'
import tlpmCustomerBillRef from '@/views/ProjectManage/pmCustomerBill/refList.vue'
import tlErpAccountPeriod from '@/views/ProjectManage/pmCustomer/erpAccountPeriod.vue'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
import { regionData, CodeToText, TextToCode } from '../../../utils/deal_area'
import tlHelp from '@/components/help/help'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlpmProjectRef, tlpmCustomerRef, tlpmCustomerBillRef, tlpmInvoiceRecordRef, tlHelp, tlErpAccountPeriod },
  data() {
    return {
      placeholder: '项目名称：******\n项目地点：******',
      moduleTitle: '项目合同开票申请',
      urlPrefix: '/pm/invoiceApply/',
      url: {
        // 额外的url
        getInvoiceApplyById: '/pm/invoiceApply/getInvoiceApplyById'
      },
      areaData: regionData,
      CodeToText: CodeToText,
      TextToCode: TextToCode,
      selectedAreas: [],
      disModifyFlag: true,
      disSaveForModify: true,
      modifyFlag: 0,
      disableAttachmentButton: true
    }
  },
  watch: {
    'model.projectcode'(newCode, oldCode) {
      var params = {
        projectidfrominvoice: this.model.projectid,
        sidx: 'sortcode',
        sord: 'asc',
        currentPage: 1,
        pageSize: 10
      }
      getAction('/pm/customer/queryErpAccountByInvoice', params).then(res => {
        if (res.success) {
          this.datas = res.result.dataList
          if (this.datas.length === 1) {
            this.$set(this.model, 'payname', this.datas[0].payname)
            this.$set(this.model, 'paymethod', this.datas[0].paymethod)
            this.$set(this.model, 'erpsourcecode', this.datas[0].erpsourcecode)
          } else {
            // 清空垃圾数据
            if (this.model.state === 0) {
              this.$set(this.model, 'payname', '')
              this.$set(this.model, 'paymethod', '')
              this.$set(this.model, 'erpsourcecode', '')
            }
          }
        }
      })
    }
  },
  computed: {
    disableJumpContract() {
      return this.stateAdd
    },
    // 状态为新增或重开
    isAddAndReadd() {
      return this.model.category === '1' || this.model.category === '3'
    },
    // 状态为作废或重开
    isCancelAndReadd() {
      return this.model.category === '2' || this.model.category === '3'
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default() {
        return ''
      }
    },
    editFlag: {
      type: Boolean,
      default: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.disModifyFlag = this.disableEditSubmit || this.disableSave
    })
  },
  methods: {
    getErpAccountPeriodData(selectionRow, selectKey, selectionValue) {
      // 账期名称
      this.$set(this.model, 'payname', selectionRow.payname)
      // 账期方式
      this.$set(this.model, 'paymethod', selectionRow.paymethod)
      // erp来源编码
      this.$set(this.model, 'erpsourcecode', selectionRow.erpsourcecode)
    },
    erpAccountPeriodDataClear() {
      this.$refs.tlErpAccountPeriod.visible = false
      this.$set(this.model, 'payname', '')
      this.$set(this.model, 'erpsourcecode', '')
      this.$set(this.model, 'paymethod', '')
    },
    openTlErpAccountPeriodDialog() {
      if (!this.model.projectid) {
        this.$message({
          message: '请先选择项目！',
          type: 'warning'
        })
        this.$refs.tlErpAccountPeriod.visible = false
      } else {
        this.$refs.tlErpAccountPeriod.visible = true
      }
    },
    jumpContract(id) {
      this.visible = false
      this.$router.push({
        name: 'pmContractIndex',
        params: {
          id: id
        }
      })
    },
    doSubmitEditForModify() {
      // 如果是修改个别字段
      if (this.modifyFlag === 1) {
        this.doSubmitModify()
        this.$nextTick(() => {
          this.visible = false
          this.modifyFlag = 0
        })
      } else {
        this.doSubmitEdit()
      }
    },
    assignDefaultValue() {
      if (this.isAdd) {
        // if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
        //     this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        // }
        // this.$set(this.model, 'orgcode', this.org.code)
        // this.$set(this.model, 'orgname', this.org.name)
        // this.$set(this.model, 'deptcode', this.dept.code)
        // this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'docdate', new Date())
        this.$set(this.model, 'isinvoicereturn', '0')
        this.$set(this.model, 'invoicecount', 1)
      }
    },
    // 排除各种容易造成垃圾数据的情况
    currentDatas() {
      if (this.model.category === '2') {
        this.$set(this.model, 'invoiceorgname', '')
        this.$set(this.model, 'invoiceorgcode', '')
        this.$set(this.model, 'invoicedeptname', '')
        this.$set(this.model, 'invoicedeptcode', '')
        // 清除垃圾数据
        this.$set(this.model, 'invoicetype', '')
        this.$set(this.model, 'invoicemoney', '')
        this.$set(this.model, 'demand', '')
        this.$set(this.model, 'explain', '')
        this.$set(this.model, 'customername', '')
        this.$set(this.model, 'companyname', '')
        this.$set(this.model, 'companyaddress', '')
        this.$set(this.model, 'taxno', '')
        this.$set(this.model, 'companytel', '')
        this.$set(this.model, 'bankname', '')
        this.$set(this.model, 'bankaccount', '')
        this.$set(this.model, 'receiver', '')
        this.$set(this.model, 'receivertel', '')
        this.$set(this.model, 'mailaddress', '')
        this.$set(this.model, 'customerid', '')
        this.$set(this.model, 'invoiceflag', '')
        this.$set(this.model, 'reinvoicereason', '')
      }
      if (this.model.category === '1') {
        // 清除垃圾数据
        this.$set(this.model, 'oldid', '')
        this.$set(this.model, 'oldinvoicecode', '')
        this.$set(this.model, 'oldnumber', '')
        this.$set(this.model, 'oldcustomername', '')
        this.$set(this.model, 'oldinvoicedate', '')
        this.$set(this.model, 'oldinvoicemoney', '')
        this.$set(this.model, 'cancelreason', '')
        this.$set(this.model, 'reinvoicereason', '')
      }
    },
    afterDelApproval() {
      this.disableAttachmentButton = false
    },
    loaded() {
      if (this.isAdd === false && this.disableEditSubmit === true) {
        var params = {
          sourceType: 'PmInvoiceApply',
          sourceId: this.model.id
        }
        this.disableAttachmentButton = false
        getAction('/cbo/attachment/calculateCount', params).then((res) => {
          if (res.success) {
            if (res.result === 0) {
              this.disableAttachmentButton = true
              this.$emit('refresh')
            }
          }
        })
      }
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
      this.$set(this.model, 'sendflag', 1)
      this.disModifyFlag = this.disableEditSubmit || this.disableSave
      this.disSaveForModify = this.disableSave
    },
    /** 执行新增提交 */
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      this.$set(this.model, 'billuid', this.uid)
      postAction(this.url.add, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.queryData(res.result.id)
            this.disableAttachmentButton = false
            this.isAdd = false
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    },
    onSelectDept(selectedDepts) {
      const selectedDept = selectedDepts[0]
      this.$set(this.model, 'invoicedeptname', selectedDept.fullname)
      this.$set(this.model, 'invoicedeptcode', selectedDept.code)
    },
    onSelectOrg(selectedOrgs) {
      const selectedOrg = selectedOrgs[0]
      this.$set(this.model, 'invoiceorgname', selectedOrg.name)
      this.$set(this.model, 'invoiceorgcode', selectedOrg.code)
    },
    /** 打开项目参照 */
    openpmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getpmProjectRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'projectid', selectionRow.id)
      this.$set(this.model, 'dispprojectid', selectionRow.name)
      this.$set(this.model, 'invoicedeptname', selectionRow.ownerdeptname)
      this.$set(this.model, 'invoicedeptcode', selectionRow.ownerdeptcode)
      this.$set(this.model, 'invoiceorgcode', selectionRow.ownerorgcode)
      this.$set(this.model, 'invoiceorgname', selectionRow.ownerorgname)
      this.$set(this.model, 'projectcode', selectionRow.code)
      this.$set(this.model, 'projectcontractmoney', selectionRow.contractmoney)
      this.$set(this.model, 'projectmoney', selectionRow.projectmoney)
      this.$set(this.model, 'projectrecbillmoney', selectionRow.recbillmoney)
      this.$set(this.model, 'projectbillmoney', selectionRow.billmoney)
      this.$set(this.model, 'deptcode', selectionRow.ownerdeptcode)
      this.$set(this.model, 'deptname', selectionRow.ownerdeptname)
      this.$set(this.model, 'orgcode', selectionRow.ownerorgcode)
      this.$set(this.model, 'orgname', selectionRow.ownerorgname)
    },
    /** 打开项目开票参照 */
    openpmInvoiceRecordRefDialog() {
      if (!this.model.projectid) {
        this.$message({
          message: '请先选择项目！',
          type: 'warning'
        })
        this.$refs.tlpmInvoiceRecordRef.visible = false
      } else {
        this.$refs.tlpmInvoiceRecordRef.visible = true
      }
    },
    /** 获取项目开票参数数据 */
    getpmInvoiceRecordRefData(selectionRows, selectKeys, selectionValues) {
      const oldapplyid = selectionRows[0].invoiceapplyid
      this.$set(this.model, 'oldid', oldapplyid)
      // 设置原发票号 oldinvoicebillmoney
      let oldinvoicecode = ''
      let oldinvoicebillmoney = ''
      let sumbillmoney = 0
      for (let { invoiceno, invoicebillmoney } of selectionRows) {
        oldinvoicecode = oldinvoicecode + invoiceno + ','
        oldinvoicebillmoney = oldinvoicebillmoney + invoicebillmoney + ','
        sumbillmoney = sumbillmoney + Math.round(parseFloat(invoicebillmoney) * 1000) / 1000
      }
      oldinvoicecode = oldinvoicecode.substring(0, oldinvoicecode.length - 1)
      oldinvoicebillmoney = oldinvoicebillmoney.substring(0, oldinvoicebillmoney.length - 1)
      this.$set(this.model, 'oldinvoicecode', oldinvoicecode)
      this.$set(this.model, 'oldinvoicebillmoney', oldinvoicebillmoney)
      const category = this.model.category
      if (category === '3') {
        // 如果是作废重开
        this.$set(this.model, 'invoicemoney', sumbillmoney)
        this.$set(this.model, 'oldinvoicemoney', sumbillmoney)
        // this.$set(this.model, 'invoiceorgcode', selectionRows[0].invoiceorgcode)
        this.fillInfosFromOldAppy(oldapplyid)
      }
    },
    fillInfosFromOldAppy(oldapplyid) {
      const param = {
        id: oldapplyid
      }
      getAction(this.url.getInvoiceApplyById, param).then((res) => {
        if (res.success) {
          const { customerid, customername, invoicetype, invoicename, demand, explain, invoiceorgname, invoiceorgcode, invoicedeptname, invoicedeptcode, erpcode, companyname, bankname, bankaccount, taxno, companyaddress, receiver, receivertel, companytel, mailaddress, mailaddress2, invoicedate, couriernumber, paymethod, payname, erpsourcecode } = res.result
          this.$set(this.model, 'customerid', customerid)
          this.$set(this.model, 'customername', customername)
          this.$set(this.model, 'invoicetype', invoicetype)
          this.$set(this.model, 'invoicetype', invoicetype)
          this.$set(this.model, 'invoicename', invoicename)
          this.$set(this.model, 'demand', demand)
          this.$set(this.model, 'explain', explain)
          this.$set(this.model, 'invoiceorgname', invoiceorgname)
          this.$set(this.model, 'invoiceorgcode', invoiceorgcode)
          this.$set(this.model, 'invoicedeptname', invoicedeptname)
          this.$set(this.model, 'invoicedeptcode', invoicedeptcode)
          this.$set(this.model, 'erpcode', erpcode)
          this.$set(this.model, 'companyname', companyname)
          this.$set(this.model, 'bankname', bankname)
          this.$set(this.model, 'bankaccount', bankaccount)
          this.$set(this.model, 'taxno', taxno)
          this.$set(this.model, 'companyaddress', companyaddress)
          this.$set(this.model, 'receiver', receiver)
          this.$set(this.model, 'receivertel', receivertel)
          this.$set(this.model, 'companytel', companytel)
          this.$set(this.model, 'mailaddress', mailaddress)
          this.$set(this.model, 'mailaddress2', mailaddress2)
          this.$set(this.model, 'oldcustomername', customername)
          this.$set(this.model, 'oldinvoicedate', invoicedate)
          this.$set(this.model, 'oldnumber', couriernumber)
          // 账期信息
          this.$set(this.model, 'paymethod', paymethod)
          this.$set(this.model, 'payname', payname)
          this.$set(this.model, 'erpsourcecode', erpsourcecode)
          this.splitAreas()
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    },
    // /** 打开项目合同参照 */
    // openpmContractRefDialog() {
    //     if (this.model.projectid == null) {
    //         this.$message({
    //             message: '请先选择项目！',
    //             type: 'warning'
    //         })
    //         this.$refs.pmContractRef.visible = false
    //     } else {
    //         this.$refs.pmContractRef.visible = true
    //     }
    // },
    // /** 获取项目合同参数数据 */
    // getpmContractRefData(selectionRow, selectKey, selectionValue) {
    //     this.$set(this.model, 'contractid', selectionRow.id)
    //     this.$set(this.model, 'dispcontractid', selectionRow.contractname)
    //     this.$set(this.model, 'invoiceorgname', selectionRow.contractorgname)
    //     this.$set(this.model, 'invoiceorgcode', selectionRow.contractorgcode)
    //     this.$set(this.model, 'invoicedeptname', selectionRow.contractdeptname)
    //     this.$set(this.model, 'invoicedeptcode', selectionRow.contractdeptcode)
    // },
    /** 打开客户名称参照 */
    openpmCustomerRefDialog() {
      this.$refs.pmCustomerRef.visible = true
    },
    /** 获取客户名称参数数据 */
    getpmCustomerRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'customerid', selectionRow.id)
      this.$set(this.model, 'customername', selectionRow.name)
      this.$set(this.model, 'erpcode', selectionRow.erpcode)
      this.$set(this.model, 'invoiceflag', '')
      this.$set(this.model, 'companyname', '')
      this.$set(this.model, 'companyaddress', '')
      this.$set(this.model, 'taxno', '')
      this.$set(this.model, 'companytel', '')
      this.$set(this.model, 'bankname', '')
      this.$set(this.model, 'bankaccount', '')
      this.$set(this.model, 'receiver', '')
      this.$set(this.model, 'receivertel', '')
      this.$set(this.model, 'mailaddress', '')
    },
    /** 打开开票标识参照 */
    openpmCustomerBillRefDialog() {
      if (this.model.customerid == null) {
        this.$message({
          message: '请先选择客户名称！',
          type: 'warning'
        })
        this.$refs.pmCustomerBillRef.visible = false
      } else {
        this.$refs.pmCustomerBillRef.visible = true
      }
    },
    /** 获取开票标识参数数据 */
    getpmCustomerBillRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'invoiceflag', selectionRow.id)
      this.$set(this.model, 'companyname', selectionRow.companyname)
      this.$set(this.model, 'companyaddress', selectionRow.companyaddress)
      this.$set(this.model, 'taxno', selectionRow.taxno)
      this.$set(this.model, 'companytel', selectionRow.companytel)
      this.$set(this.model, 'bankname', selectionRow.bankname)
      this.$set(this.model, 'bankaccount', selectionRow.bankaccount)
      this.$set(this.model, 'receiver', selectionRow.receiver)
      this.$set(this.model, 'receivertel', selectionRow.receivertel)
      this.$set(this.model, 'mailaddress2', selectionRow.mailaddress2 || selectionRow.mailaddress)
      this.$set(this.model, 'mailaddress', selectionRow.mailaddress)
      this.splitAreas()
    },
    /** 提交前执行 */
    beforeSubmit() {
      if (this.isAddAndReadd && this.selectedAreas.length === 0) {
        this.$tap.alert('请选择地址中的省市区')
        return false
      }
      this.joinAreas()
      if (this.model.category === '2') {
        this.$set(this.model, 'invoiceorgname', '')
        this.$set(this.model, 'invoiceorgcode', '')
        this.$set(this.model, 'invoicedeptname', '')
        this.$set(this.model, 'invoicedeptcode', '')
        // 清除垃圾数据
        this.$set(this.model, 'invoicetype', '')
        this.$set(this.model, 'invoicemoney', '')
        this.$set(this.model, 'invoiceunit', '')
        this.$set(this.model, 'invoicecount', '')
        this.$set(this.model, 'invoiceamount', '')
        this.$set(this.model, 'demand', '')
        this.$set(this.model, 'explain', '')
        this.$set(this.model, 'customername', '')
        this.$set(this.model, 'companyname', '')
        this.$set(this.model, 'companyaddress', '')
        this.$set(this.model, 'taxno', '')
        this.$set(this.model, 'companytel', '')
        this.$set(this.model, 'bankname', '')
        this.$set(this.model, 'bankaccount', '')
        this.$set(this.model, 'receiver', '')
        this.$set(this.model, 'receivertel', '')
        this.$set(this.model, 'mailaddress', '')
        this.$set(this.model, 'customerid', '')
        this.$set(this.model, 'invoiceflag', '')
        this.$set(this.model, 'reinvoicereason', '')
      }
      if (this.model.category === '1') {
        // 清除垃圾数据
        this.$set(this.model, 'oldid', '')
        this.$set(this.model, 'oldinvoicecode', '')
        this.$set(this.model, 'oldnumber', '')
        this.$set(this.model, 'oldcustomername', '')
        this.$set(this.model, 'oldinvoicedate', '')
        this.$set(this.model, 'oldinvoicemoney', '')
        this.$set(this.model, 'cancelreason', '')
        this.$set(this.model, 'reinvoicereason', '')
      }
      // 申请开票金额 合同总金额 已开票金额
      let { invoicemoney, projectcontractmoney, projectbillmoney, oldinvoicebillmoney } = this.model
      invoicemoney = invoicemoney == null ? 0 : invoicemoney
      projectcontractmoney = projectcontractmoney == null ? 0 : projectcontractmoney
      projectbillmoney = projectbillmoney == null ? 0 : projectbillmoney
      oldinvoicebillmoney = oldinvoicebillmoney == null ? 0 : oldinvoicebillmoney
      let a = invoicemoney + projectbillmoney - oldinvoicebillmoney
      let b = (Math.floor(a * 100) / 100)
      if (b > projectcontractmoney) {
        this.$tap.alert('开票金额与已开票金额之和不能高于合同总金额！', 'warning')
        return false
      }
      // if ((invoicemoney || invoicemoney === 0) && (projectcontractmoney || projectcontractmoney === 0) && (projectbillmoney || projectbillmoney === 0) && (oldinvoicebillmoney || oldinvoicebillmoney === 0)) {
      //   // if (invoicemoney > contractamount) {
      //   //     this.$tap.alert("开票金额不能高于当前合同金额！", "warning")
      //   //     return false
      //   // }
      //   if (invoicemoney + projectbillmoney - oldinvoicebillmoney > projectcontractmoney) {
      //     this.$tap.alert('开票金额与已开票金额之和不能高于合同总金额！', 'warning')
      //     return false
      //   }
      // }
      return true
    },
    /** 编辑，发请求获得数据 */
    queryData(id) {
      if (!this.url.load) {
        this.$tap.alert('请设置load地址!', 'warning')
        return
      }
      return getAction(this.url.load, { id: id }).then((res) => {
        if (res.success) {
          this.model = res.result
          this.splitAreas()
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    },
    splitAreas() {
      const address = this.model.mailaddress2 || this.model.mailaddress
      const addressArray = address.split('$')
      if (addressArray.length > 3) {
        let selectedAreaCodes = []
        // 省编码
        const province = addressArray[0]
        const provinceCode = this.TextToCode[province].code
        selectedAreaCodes.push(provinceCode)
        // 市编码
        const city = addressArray[1]
        const cityCode = this.TextToCode[province][city].code
        selectedAreaCodes.push(cityCode)
        // 区编码
        const area = addressArray[2]
        const areaCode = this.TextToCode[province][city][area].code
        selectedAreaCodes.push(areaCode)
        this.$set(this, 'selectedAreas', selectedAreaCodes)
        // 邮寄地址详细部分
        this.$set(this.model, 'detailmailaddress', addressArray[3])
      } else {
        this.$set(this.model, 'detailmailaddress', addressArray[0])
      }
    },
    changeArea(value) {
      // 调试使用
      console.log(value)
    },
    joinAreas() {
      let proAndCityAndArea = ''
      let proAndCityAndAreaWithDelimiter = ''
      for (let selectedArea of this.selectedAreas) {
        proAndCityAndArea += this.CodeToText[selectedArea]
        proAndCityAndAreaWithDelimiter = proAndCityAndAreaWithDelimiter + this.CodeToText[selectedArea] + '$'
      }
      if (this.model.detailmailaddress) {
        proAndCityAndArea += this.model.detailmailaddress
        proAndCityAndAreaWithDelimiter += this.model.detailmailaddress
      }
      this.model.mailaddress = proAndCityAndArea
      this.model.mailaddress2 = proAndCityAndAreaWithDelimiter
    },
    /** 关闭页面时，刷新主页面*/
    close() {
      console.log(222)
      this.$emit('refresh')
      this.selectedAreas = []
    },
    // 修改开票金额
    changeInvoicemoney(value) {
      const { invoicecount, invoiceamount } = this.model
      if (invoicecount && value) {
        // 如果数量不为空，则根据 开票金额/数量=单价
        const newInvoiceamount = (value / invoicecount).toFixed(6)
        this.$set(this.model, "invoiceamount", newInvoiceamount)
      } else if (invoiceamount && value) {
        // 如果数量为空，并且单价不为空，则根据 开票金额/单价=数量
        const newInvoicecount = (value / invoiceamount).toFixed(2)
        this.$set(this.model, "invoicecount", newInvoicecount)
      }
    },
    // 修改开票单价，根据数量计算开票金额
    changeInvoiceamount(value) {
      const { invoicecount, invoicemoney } = this.model
      if (invoicecount && value) {
        // 如果数量不为空，则根据 单价 * 数量 = 开票金额
        const newInvoicemoney = (value * invoicecount).toFixed(2)
        this.$set(this.model, "invoicemoney", newInvoicemoney)
      } else if (invoicemoney && value) {
        // 如果数量为空，则根据 开票金额 / 单价 = 数量
        const newInvoicecount = (invoicemoney / value).toFixed(2)
        this.$set(this.model, "invoicecount", newInvoicecount)
      }
    },
    // 修改开票数量
    changeInvoicecount(value) {
      const { invoicemoney, invoiceamount } = this.model
      if (invoiceamount && value) {
        // 如果单价有值，则根据 单价 * 数量 = 开票金额
        const newInvoicemoney = (value * invoiceamount).toFixed(2)
        this.$set(this.model, "invoicemoney", newInvoicemoney)
      } else if (invoicemoney && value) {
        // 如果单价为空，则根据 开票金额 / 数量 = 单价
        const newInvoiceamount = (invoicemoney / value).toFixed(6)
        this.$set(this.model, "invoiceamount", newInvoiceamount)
      }
    },
    // 修改开票信息
    doModify() {
      // 将发票号、是否快递、快递方式、快递单号字段置为可修改，且保存按钮可用
      this.disModifyFlag = false
      this.disSaveForModify = false
      this.modifyFlag = 1
    },
    // 修改特定字段
    doSubmitModify() {
      // 组装特定字段
      var updateParam = {}
      updateParam.id = this.model.id
      updateParam.invoiceno = this.model.invoiceno
      updateParam.sendflag = this.model.sendflag
      updateParam.courier = this.model.courier
      updateParam.couriernumber = this.model.couriernumber
      postAction("/pm/invoiceApply/modifyDoc", updateParam).then((res) => {
        this.$tap.alert(res.message)
      })
    }
  }
}
</script>
